package sz.szsmk.citizencard.util;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.chenenyu.router.Router;

import sz.szsmk.citizencard.DaggerApplication;

/**
 * 页面跳转控制类
 */
public class JumpUtils {
    public static void jump(Context ctx, String url, int isNeedLogin) {//不适用与web页面
          boolean isNeed = false;
        isNeed = isNeedLogin == 1;
        Bundle bundle = new Bundle();
        if (TextUtils.isEmpty(url))
            return;
        if (isNeed) {
            if (StringUtils.isEmpty(ACache.get(ctx).getAsString("access_token"))) {
                //跳转登录页
                bundle.putString("url", url);
                bundle.putBoolean("isNeedLogin", isNeed);
                Router.build("login").with(bundle).go(ctx);
                return;
            }
        }
        if (url.startsWith("http")) {
            //跳转至web页面
            Router.build("web").with("url", url).go(ctx);
//            ARouter.getInstance().build(url).navigation();
        } else {
            Router.build(url).go(ctx);

//            ARouter.getInstance().build(url).navigation();
        }
    }

    /**
     * 带参数原生跳转
     */
    public static void jumptoNatvie(Context ctx, String url, int isNeedLogin, Bundle bundle) {
        boolean isNeed = false;
        isNeed = isNeedLogin == 1;
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (isNeed) {
//            if (!DaggerApplication.getIns().isLogin()) {
            if (StringUtils.isEmpty(ACache.get(ctx).getAsString("access_token"))) {
                //跳转登录页
                bundle.putString("url", url);
                bundle.putBoolean("isNeedLogin", isNeed);
                Router.build("login").with(bundle).go(ctx);

                return;
            }
        }
        Router.build(url).with(bundle).go(ctx);
    }

    public static void gotoNeedGesture(Context ctx, String url,Bundle bundle) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        if (StringUtils.isBlank(access_token)) {
            JumpUtils.jump(ctx, "login", 0);
            return;
        }
        if ("1".equals(Constant.isOpen)) {
//            Bundle bundle = new Bundle();
            bundle.putString("url", url);
            //需要校验token失效
            JumpUtils.jumptoNatvie(ctx, "gestureLoginActivity", 0, bundle);
        } else {

            JumpUtils.jumptoNatvie(ctx, url, 0,bundle);
        }
    }


    /**
     * 带参数原生跳转
     */
    public static void jumptoNatvie(Context ctx, String url, int isNeedLogin, Bundle bundle,int requestCode) {
        boolean isNeed = false;
        isNeed = isNeedLogin == 1;
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (isNeed) {
            if (!DaggerApplication.isLogin()) {
                //跳转登录页
                bundle.putString("url", url);
                bundle.putBoolean("isNeedLogin", isNeed);
                Router.build("login").with(bundle).go(ctx);

                return;
            }
        }
        Router.build(url).requestCode(requestCode).with(bundle).go(ctx);
    }
}
